package com.ruoyi.personnel.salary.tax.service;

import com.ruoyi.personnel.salary.tax.domain.TaxDomain;
import com.ruoyi.personnel.salary.tax.domain.TaxDto;
import com.ruoyi.personnel.salary.tax.mapper.TaxMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
/**
 * 工资所得税
 * 业务处理层
 */
@Service
public class TaxServiceImpl implements ITaxService {
    @Autowired
    private TaxMapper taxMapper;

    /**
     * 获取所有工资所得税记录
     * @return
     */
    @Override
    public List<TaxDto> selectIncomeTaxList() {
        List<TaxDto> taxDtos = taxMapper.selectTaxList();
        if (!taxDtos.isEmpty()) {
            // 速算扣除值的计算
            taxDtos.get(0).setEasyCalculationValue(0.00);
            for (int i = 1; i < taxDtos.size(); i++) {
                Double easyCalculationValue =
                        taxDtos.get(i - 1).getMaxValue() * (taxDtos.get(i).getTaxRate() - (taxDtos.get(i - 1).getTaxRate())) + (taxDtos.get(i - 1).getEasyCalculationValue());
                taxDtos.get(i).setEasyCalculationValue(easyCalculationValue);
            }
        }
        return taxDtos;
    }

    /**
     * 新增工资所得税
     * @param tax
     * @return
     */
    @Override
    public int insertIncomeTax(TaxDomain tax) {
        return taxMapper.insertTax(tax);
    }

    /**
     * 修改工资所得税
     * @param tax
     * @return
     */
    @Override
    public int updateIncomeTax(TaxDomain tax) {
        return taxMapper.updateTax(tax);
    }

    /**
     * 删除工资所得税
     * 仅修改删除标志
     * @param tax
     * @return
     */
    @Override
    public int updateIncomeTaxDeleteFlag(TaxDomain tax) {
        return taxMapper.updateTaxDeleteFlag(tax);
    }

    /**
     * 计算个人工资所得税
     * @param price
     * @return
     */
    @Override
    public Double calculateIncomeTax(Double price) {
        Double IncomeTax = 0.00;
        if (price >= 0 && price <= 3000) {
            IncomeTax = price * 0.03;
        } else if (price >= 3001 && price <= 12000) {
            IncomeTax = (price - 3000) * 0.1 + 3000 * 0.03;
        } else if (price >= 12001 && price <= 25000) {
            IncomeTax = (price - 12000) * 0.2 + 9000 * 0.1 + 3000 * 0.03;
        }
        return IncomeTax;
    }
}
